package com.ctrlspace.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONAware;
import org.json.simple.JSONObject;

public class FileUnit implements Comparable<FileUnit>, JSONAware {
	private int fileId;
	private int creatorId;
	private String path;
	private String fileName;
	private String label;
	private String content;
	private FileType type;
	private Date dateCreated;
	private Date lastModified;
	private int lockOwnerId;
	private int projectId;
	private List<FileUnit> children = new ArrayList<FileUnit>();

	/**
	 * @return the fileId
	 */
	public int getFileId() {
		return fileId;
	}

	/**
	 * @param fileId
	 *            the fileId to set
	 */
	public void setFileId(int fileId) {
		this.fileId = fileId;
	}

	/**
	 * @return the creatorId
	 */
	public int getCreatorId() {
		return creatorId;
	}

	/**
	 * @param creatorId
	 *            the creatorId to set
	 */
	public void setCreatorId(int creatorId) {
		this.creatorId = creatorId;
	}

	/**
	 * @return the path
	 */
	public String getPath() {
		return path;
	}

	/**
	 * @param path
	 *            the path to set
	 */
	public void setPath(String path) {
		this.path = path;
	}

	/**
	 * @return the fileName
	 */
	public String getFileName() {
		return fileName;
	}

	/**
	 * @param fileName
	 *            the fileName to set
	 */
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getLabel() {
		if (label == null) {
			return fileName;
		}
		return label;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the type
	 */
	public FileType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(FileType type) {
		this.type = type;
	}

	/**
	 * @return the dateCreated
	 */
	public Date getDateCreated() {
		return dateCreated;
	}

	/**
	 * @param dateCreated
	 *            the dateCreated to set
	 */
	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	/**
	 * @return the lastModified
	 */
	public Date getLastModified() {
		return lastModified;
	}

	/**
	 * @param lastModified
	 *            the lastModified to set
	 */
	public void setLastModified(Date lastModified) {
		this.lastModified = lastModified;
	}

	/**
	 * @return the lockOwnerId
	 */
	public int getLockOwnerId() {
		return lockOwnerId;
	}

	/**
	 * @param lockOwnerId
	 *            the lockOwnerId to set
	 */
	public void setLockOwnerId(int lockOwnerId) {
		this.lockOwnerId = lockOwnerId;
	}

	public int getProjectId() {
		return projectId;
	}

	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}

	public void setChildren(List<FileUnit> children) {
		this.children = children;
	}

	public List<FileUnit> getChildren() {
		return children;
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof FileUnit)) {
			return false;
		}
		
		FileUnit that = (FileUnit) obj;
		if (that.getFileId() != this.getFileId()) {
			return false;
		}
		
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String toJSONString() {
		Map out = new LinkedHashMap();
		out.put("id", path);
		out.put("path", path);
		out.put("label", getLabel());
		out.put("type", type);
		out.put("iconClass", "csIcons " + type.getIconName() + "Icon");
		out.put("projectId", projectId);
		if (content != null) {
			out.put("content", content);
		}
		if (children != null && children.size() > 0) {
			out.put("children", children);
		}

		return JSONObject.toJSONString(out);
	}

	@Override
	public int compareTo(FileUnit target) {
		return getPath().compareTo(target.getPath());
	}
}
